package com.base.myproject.client.HR.attendance;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.base.myproject.client.busi.BaseStore;
import com.base.myproject.client.busi.BO.BusiObject;
import com.base.myproject.client.jdbc.StoreInfo;
import com.base.myproject.client.tools.Tools;
import com.base.myproject.client.ui.BaseGridPanel;
import com.base.myproject.client.ui.BaseJDBCPanel;
import com.base.myproject.client.ui.celleditor.BaseComboBoxX;
import com.extjs.gxt.ui.client.Style.SelectionMode;
import com.extjs.gxt.ui.client.data.ModelType;
import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.ComponentEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.GridEvent;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.VerticalPanel;
import com.extjs.gxt.ui.client.widget.form.DateField;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.grid.CellEditor;
import com.extjs.gxt.ui.client.widget.grid.CheckBoxSelectionModel;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.google.gwt.i18n.client.DateTimeFormat;

/**
 * @author cxghiu410@gmail.com
 * 用餐时段设置，已经完善了
 */
public class AttendanceEatTime extends BaseJDBCPanel {
	
	TextField<String> txt1 = new TextField<String>();
	TextField<String> txt2 = new TextField<String>();
	public AttendanceEatTime() {
		init();
	}

	public void init() {
		initWeigetx();
		registerMainStore(store);
	}
	
	public void complete() {
		super.complete();
	}

	public void initWeigetx() {
		VerticalPanel v = new VerticalPanel();
		v.setSpacing(2);
		v.add(createGrid());
		this.add(v);
	}

	BaseStore<BusiObject> store = null;

	BaseStore<BusiObject> getStore() {
		if (store == null) {
			createStore();
		}
		return store;
	}

	BaseStore<BusiObject> createStore() {
		ModelType type = new ModelType();
		type.setRoot("records");
		type.setRecordName("record");
		type.setTotalName("totle");
		type.addField("icode");
		type.getField("icode").setType(Long.class);
		type.addField("name");
		type.getField("name").setType(String.class);
		type.addField("bgnTime");
		type.getField("bgnTime").setType(String.class);
		type.addField("endTime");
		type.getField("endTime").setType(String.class);
		type.addField("isUse");
		type.getField("isUse").setType(String.class);
		StoreInfo info = new StoreInfo();
		info.setTablename("hr_Attendance_eatTime");
		info.setKeyname(new String[] { "icode"});
		info.setSql("select * from hr_Attendance_eatTime");
		store = new BaseStore<BusiObject>(type, info);
		return store;
	}

	BaseGridPanel grid = null;

	BaseGridPanel createGrid() {
		CheckBoxSelectionModel cbs = new CheckBoxSelectionModel();
		List<ColumnConfig> columns = new ArrayList<ColumnConfig>();
		columns.add(cbs.getColumn());

		ColumnConfig C2 = new ColumnConfig("name", "时段名", 100);
		C2.setEditor(new CellEditor(new TextField<String>()));
		columns.add(C2);
		
		ColumnConfig C3 = new ColumnConfig("bgnTime", "开始时间", 140);
		txt1.addListener(Events.Change, getListener());
		C3.setEditor(new CellEditor(txt1));
		columns.add(C3);
		
		ColumnConfig C5 = new ColumnConfig("endTime", "结束时间", 140);
		txt2.addListener(Events.Change, getListener());
		C5.setEditor(new CellEditor(txt2));
		columns.add(C5);
		
		ColumnConfig C4 = new ColumnConfig("isUse", "是否有效", 180);
		BaseComboBoxX bc1 = new BaseComboBoxX("name","dictionary", "where type='use'");
		C4.setEditor(bc1.getCellEditor());
		columns.add(C4);

		ColumnModel cm = new ColumnModel(columns);
		grid = new BaseGridPanel(getStore(), cm, true, true);
		cbs.setSelectionMode(SelectionMode.SINGLE);
		grid.getBg().setSelectionModel(cbs);
		Tools.setResize(grid.getBg(), 0.6, 0.6);
		return grid;
	}
	
	boolean checkTime(TextField<String> tf) {
		String bt1 = tf.getValue();
		char[] ch = bt1.toCharArray();
		if (ch.length != 5) {
			return false;
		}
		int num1 = ch[0];
		int num2 = ch[1];
		int num3 = ch[2];
		int num4 = ch[3];
		int num5 = ch[4];
		if (num1 > 51 || num1 < 47 || num2 < 47 || num3 > 58 || num3 != 58
				|| num4 < 47 || num4 > 54 || num5 < 47 || num5 > 58) {
			return false;
		}
		return true;
	}
	
	/*
	 * 验证开始时间与结束时间的大小
	 */
	boolean checkSize(String[] t1, String[] t2) {
		if (t1.equals(t2)) {
			return false;
		}
		if (Integer.parseInt(t1[0]) > Integer.parseInt(t2[0])) {
			return false;
		} else if (Integer.parseInt(t1[0]) == Integer.parseInt(t2[0])) {
			if (Integer.parseInt(t1[1]) >= Integer.parseInt(t2[1])) {
				return false;
			}
		}
		return true;
	}
	
	int sync = 0;
	Listener<BaseEvent> defaultlistener;
	private int index = -100;

	Listener<BaseEvent> getListener() {
		if (defaultlistener != null) {
			return defaultlistener;
		}
		defaultlistener = new Listener<BaseEvent>() {
			public void handleEvent(BaseEvent be) {
				sync++;
				try {
					if (sync == 1) {
						if (be.getType() == Events.OnClick) {
							
						} else if (be.getType() == Events.Change) {
							if(be.getSource().equals(txt1)){
								if (!checkTime(txt1)) {
									txt1.setValue("");
									MessageBox.alert("警告","输入的开始时间不合法", null);
								}
							}else if(be.getSource().equals(txt2)){
								if(!checkTime(txt2)){
									txt2.setValue("");
									MessageBox.alert("警告","输入的结束时间不合法", null);
								}else{
									String[] t1 = grid.getBg().getSelectionModel().getSelectedItem().get("bgnTime").toString().split(":");
									String[] t2 = txt2.getValue().split(":");
									if(Integer.parseInt(t1[0]) > Integer.parseInt(t2[0])){
										txt2.setValue("");
										MessageBox.alert("警告","结束时间不能小于开始时间", null);
									}
								}
							}
						} 
						else if (be.getType() == Events.KeyUp) {

							if (be instanceof ComponentEvent) {
								if (((ComponentEvent) be).getKeyCode() == 13) // 回车
								{
								}
							}
						}
					}
				} finally {
					sync--;
				}
			}

		};
		return defaultlistener;
	}
	
}
